package com.southminority.ethnic.controller.student;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.student.vo.ActivityDetailVo;
import com.southminority.ethnic.controller.student.vo.ActivityPageVo;
import com.southminority.ethnic.controller.student.vo.StudentPageReqVo;
import com.southminority.ethnic.service.student.StudentActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学生活动用户端控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "学生活动模块（用户端）", description = "学生活动查询接口")
@RestController
@RequestMapping("/student/activity")
@CrossOrigin
public class StudentActivityController {
    
    @Autowired
    private StudentActivityService studentActivityService;

    /**
     * 分页查询学生活动列表（优化版，不返回完整内容）
     * @param reqVo 分页请求参数
     * @return 分页结果
     */
    @Operation(summary = "分页查询学生活动列表")
    @PostMapping("/page")
    public R<Page<ActivityPageVo>> getActivityPage(@Valid @RequestBody StudentPageReqVo reqVo) {
        try {
            Page<ActivityPageVo> result = studentActivityService.getActivityPage(
                reqVo.getPageNum(), 
                reqVo.getPageSize(), 
                reqVo.getKeyword(),
                    1
            );
            return R.ok(result);
        } catch (Exception e) {
            log.error("查询学生活动列表失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询学生活动详情（包含完整内容和图片）
     * @param id 活动ID
     * @return 活动详情
     */
    @Operation(summary = "查询学生活动详情")
    @GetMapping("/{id}")
    public R<ActivityDetailVo> getActivityById(@PathVariable Long id) {
        try {
            ActivityDetailVo activity = studentActivityService.getActivityDetail(id);
            if (activity == null) {
                return R.fail("活动信息不存在或已下架");
            }
            return R.ok(activity);
        } catch (Exception e) {
            log.error("查询学生活动详情失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
}
